#!/usr/bin/env bash

tmp=scripts/cache/dunst-history.json
lock="scripts/cache/dunst-toggle.lock"
lockinfo="scripts/cache/dunst-lock-info"

touch $lockinfo

declare ids
export toggle_icon=""

sanitize() {
  retstr=$(echo $1 | sed 's/\\\\/\\\\\\\\/g' | sed 's/"/\\"/g')

  # echo "original: $1"
  echo "$retstr"
  # echo 'sanitized'
}

get_ids() {
  mapfile -t ids < <(dunstctl history | gojq -r ".data[] | .[] | select(.appname.data != \"Spotify\") | .id.data")
}

get_notif() {
  echo -n '['

  for id in "${ids[@]}"; do
      mapfile -t n < <(gojq -r ".data[] | .[] | select(.id.data == $id) | .appname.data, .summary.data, .timestamp.data, .body.data" "$tmp" | sed -r '/^\s*$/d' | sed -e 's/\%/ percent/g')
      ohkay=$(sanitize "${n[1]}")
      time=$(get_time ${n[2]})
      # echo $ohkay
      echo -n ''$([ $id -eq ${ids[0]} ] || echo ,)' { '
      echo -n '"id": "'"$id"'", "appname": "'"${n[0]}"'", "summary": "'"$ohkay"'", "time": "'"$time"'", "body": "'"${n[3]}"'"'
      echo -n '}' 
  done

  echo ']'
}

toggle() {
  dunstctl set-paused toggle

  if [ ! -f "$lock" ]; then
    export toggle_icon=""
    touch "$lock"
  else
    export toggle_icon=""
    rm "$lock"
  fi

  echo "icon_change" > $lockinfo
}

clear() {
  dunstctl history-clear
  echo "icon_change" > $lockinfo
}

get_icon() {
  if [ ${#ids[@]} -eq 0 ]; then
    echo ""
  else
    echo ""
  fi
}

get_time(){
    system_timestamp="$(cut -d'.' -f1 /proc/uptime)"
    how_long_ago=$((system_timestamp - $1 / 1000000))
    notification_time="$(date +%R -d "$how_long_ago seconds ago")"
    echo $notification_time
}

if [ "$1" == "toggle" ]; then
  toggle
  dunstctl history > "$tmp"
elif [ "$1" == "clear" ]; then
  clear
  dunstctl history > "$tmp"
elif [ "$1" == "icons" ]; then
  dunstctl history > "$tmp"
  get_ids
  is_paused=$(dunstctl is-paused)
  echo '{"toggle_icon": "'"$toggle_icon"'", "paused": '$is_paused', "icon": "'"$(get_icon)"'"}'
  tail -f "$lockinfo" | while read -r; do
    get_ids
    is_paused=$(dunstctl is-paused)
    echo '{"toggle_icon": "'"$toggle_icon"'", "paused": '$is_paused', "icon": "'"$(get_icon)"'"}'
  done
elif [ "$1" == "get_time" ]; then
    get_time $2
else
  while true; do
    dunstctl history > "$tmp"
    get_ids
    get_notif
    # dunstctl history
    # eww update notification_revcnt=$(dunstctl count history)
    sleep 1
  done
  # get_ids
  # scripts/notifget
  # tail -f "$tmp" 2>/dev/null | rg --line-buffered "aa\{sv\}" | while read -r; do
  #   get_ids
  #   scripts/notifget
  # done
fi
